2 * Copyright (c) 2019-2020 Apple Inc. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "unittest_common.h"
18 #import <XCTest/XCTest.h>
20 #import <NetworkExtension/NEPolicySession.h>
22 @interface PathEvaluationTest : XCTestCase
27 @implementation PathEvaluationTest
31 mDNSPlatformMemZero(&mDNSStorage, sizeof(mDNS));
32 init_mdns_environment(mDNStrue);
41 if(!getenv("DNSSDUTIL_XCTEST")) return; // Don't run without this environment variable
43 mDNSInterfaceID routableIndex;
45 mDNSPlatformMemZero(&q, sizeof(DNSQuestion));
46 q.TargetQID.NotAnInteger = 1;
48 q.InterfaceID = if_nametoindex( "pdp_ip0" );
49 fprintf(stdout, "%s %s with cellular index %d named pdp_ip0\n", q.InterfaceID ? "Starting" : "Exiting (no cellular interface)", __FUNCTION__, q.InterfaceID);
50 if (!q.InterfaceID) return;
52 routableIndex = IndexForInterfaceByName_ut( "pdp_ip0" );
53 fprintf(stdout, "Testing blocked by (%s)\n", routableIndex ? "policy" : "no route");
55 mDNSPlatformGetDNSRoutePolicy(&q);
56 XCTAssertFalse(q.BlockedByPolicy);
59 NSMutableArray *routeRules = [NSMutableArray array];
60 NEPolicyRouteRule *routeRule = [NEPolicyRouteRule routeRuleWithAction:NEPolicyRouteRuleActionDeny forType:NEPolicyRouteRuleTypeCellular];
61 [routeRules addObject:routeRule];
62 routeRule = [NEPolicyRouteRule routeRuleWithAction:NEPolicyRouteRuleActionDeny forType:NEPolicyRouteRuleTypeWiFi];
63 [routeRules addObject:routeRule];
64 routeRule = [NEPolicyRouteRule routeRuleWithAction:NEPolicyRouteRuleActionDeny forType:NEPolicyRouteRuleTypeWired];
65 [routeRules addObject:routeRule];
67 NEPolicyResult *result = [NEPolicyResult routeRules:routeRules];
68 NEPolicy *policy = [[NEPolicy alloc] initWithOrder:1 result:result conditions:@[ [NEPolicyCondition effectivePID:q.pid], [NEPolicyCondition allInterfaces] ]];
70 NEPolicySession *policySession = [[NEPolicySession alloc] init];
71 XCTAssertNotNil(policySession, "Check entitlemnts");
72 [policySession addPolicy:policy];
73 [policySession apply];
75 mDNSPlatformGetDNSRoutePolicy(&q);
76 // Either if these asserts indicate a regression in mDNSPlatformGetDNSRoutePolicy
77 if (routableIndex) XCTAssertTrue(q.BlockedByPolicy, "blocked by (policy) test failure");
78 else XCTAssertFalse(q.BlockedByPolicy, "blocked by (no route) test failure");
80 [policySession removeAllPolicies];
81 [policySession apply];
82 fprintf(stdout, "Completed %s\n", __FUNCTION__);